package com.example.springboot_demo.demos.web.book_work.Controller;

import com.example.springboot_demo.Common.Result;
import com.example.springboot_demo.demos.web.book_work.Pojo.dto.BrrowBookDto;
import com.example.springboot_demo.demos.web.book_work.Pojo.dto.ReturnBookDto;
import com.example.springboot_demo.demos.web.book_work.Server.BookServer;
import com.example.springboot_demo.demos.web.book_work.Server.BookUserServer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "书本相关接口")
@RestController
@RequestMapping("/api/book")
public class BookController {

    @Autowired
    private BookServer bookServer;

    @Autowired
    private BookUserServer bookUserServer;

    @GetMapping
    @ApiOperation("获取全部书籍信息")
    public Result getBookList(){
        return Result.success(bookServer.getBookList());
    }

    @GetMapping("/{id}")
    @ApiOperation("测试接口")
    public Result getBookById(@PathVariable Integer id){
        return Result.success(bookUserServer.getUser(id));
    }

    @PostMapping("/borrow")
    @ApiOperation("借书接口")
    public Result borrowBook(@RequestBody BrrowBookDto brrowBookDto){
        Boolean result = bookServer.borrowBook(brrowBookDto);
        if (result){
            return Result.success();
        }
        return Result.error(result.toString());
    }


    @PostMapping("/return")
    @ApiOperation("还书接口")
    public Result returnBook(@RequestBody ReturnBookDto returnBookDto){
        return Result.success("罚金:" + bookServer.returnBook(returnBookDto));
    }
}
